package com.utils;

import com.model.TSysOrg;
import com.modeldiy.OrgNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 树状节点处理
 */
public class TreeNodeUtils {

    /**
     *
     * @param id
     * @return
     */
    public static void getChildren(Long id,List childrenList,List list){
        for(int i=0;i<list.size();i++){
            OrgNode node=(OrgNode)list.get(i);
            if(!childrenList.contains(node)) {//如果还没有纳入子节点
                if(node!=null&&node.getPid()==id){//如果是子节点
                    childrenList.add(node);//纳入子节点
                    getChildren(node.getId(),childrenList,list);//检查子节点的子节点
                }
            }
        }
    }


    public static void main (String[] args) {
        List childrenList=new ArrayList<>();
        List list=new ArrayList<>();
        OrgNode node;
        node=new OrgNode( Long.valueOf(1) , Long.valueOf(0),"h1");
        list.add(node);
        node=new OrgNode( Long.valueOf(2) , Long.valueOf(1),"h1");
        list.add(node);
        node=new OrgNode( Long.valueOf(3) , Long.valueOf(2),"h1");
        list.add(node);
        node=new OrgNode( Long.valueOf(4) , Long.valueOf(3),"h1");
        list.add(node);
        node=new OrgNode( Long.valueOf(5) , Long.valueOf(1),"h1");
        list.add(node);
        node=new OrgNode( Long.valueOf(6) , Long.valueOf(1),"h1");
        list.add(node);
        node=new OrgNode( Long.valueOf(7) , Long.valueOf(1),"h1");
        list.add(node);
        node=new OrgNode( Long.valueOf(8) , Long.valueOf(5),"h1");
        list.add(node);
        node=new OrgNode( Long.valueOf(9) , Long.valueOf(5),"h1");
        list.add(node);

//        for(int i=0;i<list.size();i++){
//            System.out.println(((OrgNode)list.get(i)).getId());
//        }

        getChildren(Long.valueOf(0),childrenList,list);
        for(int i=0;i<childrenList.size();i++){
            System.out.println(((OrgNode)childrenList.get(i)).getId());
        }

    }
}
